package com.foreknow.elm.controller;

import com.foreknow.elm.po.DeliveryAddress;
import com.foreknow.elm.service.DeliveryAddressService;
import com.foreknow.elm.service.impl.DeliveryAddressServiceImpl;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

public class DeliveryAddressController {
    /**
     * 功能：根据用户编号查询所属送货地址
     *
     * @param request
     * @return
     */
    public Object listDeliveryAddressByUserId(HttpServletRequest request) {
        String userId = request.getParameter("userId");
        DeliveryAddressService deliveryAddressService = new DeliveryAddressServiceImpl();
        List<DeliveryAddress> list = deliveryAddressService.selectByUserId(userId);
        return list;
    }

    /**
     * 功能：根据送货地址编号查询送货地址
     *
     * @param request
     * @return
     */
    public Object getDeliveryAddressById(HttpServletRequest request) {
        String daId = request.getParameter("daId");
        DeliveryAddressService deliveryAddressService = new DeliveryAddressServiceImpl();
        DeliveryAddress deliveryAddress = deliveryAddressService.selectById(Integer.valueOf(daId));
        return deliveryAddress;
    }

    /**
     * 功能：向送货地址表中添加一条记录
     *
     * @param request
     * @return
     */
    public Object saveDeliveryAddress(HttpServletRequest request) {
        String contactName = request.getParameter("contactName");
        String contactSex = request.getParameter("contactSex");
        String contactTel = request.getParameter("contactTel");
        String address = request.getParameter("address");
        String userId = request.getParameter("userId");
        DeliveryAddressService deliveryAddressService = new DeliveryAddressServiceImpl();
        Integer integer = deliveryAddressService.saveDeliveryAddress(contactName, Integer.valueOf(contactSex), contactTel, address, userId);
        return integer;
    }

    /**
     * 功能：根据送货地址编号更新送货地址信息
     *
     * @param request
     * @return
     */
    public Object updateDeliveryAddress(HttpServletRequest request) {
        String daId = request.getParameter("daId");
        String contactName = request.getParameter("contactName");
        String contactSex = request.getParameter("contactSex");
        String contactTel = request.getParameter("contactTel");
        String address = request.getParameter("address");
        String userId = request.getParameter("userId");
        DeliveryAddressService deliveryAddressService = new DeliveryAddressServiceImpl();
        Integer integer = deliveryAddressService.updateDeliveryAddress(Integer.valueOf(daId), contactName, Integer.valueOf(contactSex), contactTel, address, userId);
        return integer;
    }

    /**
     * 功能：根据送货地址编号删除一条记录
     *
     * @param request
     * @return
     */
    public Object removeDeliveryAddress(HttpServletRequest request) {
        String daId = request.getParameter("daId");
        DeliveryAddressService deliveryAddressService = new DeliveryAddressServiceImpl();
        Integer integer = deliveryAddressService.removeDeliveryAddress(Integer.valueOf(daId));
        return integer;
    }
}
